package com.example.user.myhjjf_app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLngBounds;
import com.blankj.utilcode.util.ActivityUtils;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.model.Model_Tagging;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Acitvity_edit extends AppCompatActivity implements Serializable {
    Model_Tagging.ValueBean.ArrayBean.DetailsBean detailsBean;
    @BindView(R.id.btn_back)
    LinearLayout btnBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_zdbm)
    EditText edZdbm;
    @BindView(R.id.ed_zddz)
    EditText edZddz;
    @BindView(R.id.ed_zdmj)
    EditText edZdmj;
    @BindView(R.id.ed_zdjd)
    EditText edZdjd;
    @BindView(R.id.ed_zdwd)
    EditText edZdwd;
    @BindView(R.id.ed_zdms)
    EditText edZdms;
    @BindView(R.id.bt_add_zd)
    Button btAddZd;
    String type,zoneId,departId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acitvity_edit);
        ButterKnife.bind(this);
        Bundle bu = getIntent().getExtras();
        txTitle.setText("站点编辑");
        type = bu.getString("type");
        if (type.equals("edit")){
            detailsBean = (Model_Tagging.ValueBean.ArrayBean.DetailsBean) bu.getSerializable("detailsBean");
            edName.setText(detailsBean.getName());
            edZdbm.setText(detailsBean.getCode());
            edZddz.setText(detailsBean.getAddress());
            edZdmj.setText(String.valueOf(detailsBean.getArea()));
            edZdjd.setText(String.valueOf(detailsBean.getLat()));
            edZdwd.setText(String.valueOf(detailsBean.getLng()));
            edZdms.setText(detailsBean.getDescription());
        }else {
            edZdjd.setText(bu.getString("lat"));
            edZdwd.setText(bu.getString("lng"));
        }
    }

    @OnClick({R.id.btn_back, R.id.bt_add_zd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.bt_add_zd:
                    String name = edName.getText().toString();
                    String code = edZdbm.getText().toString();
                    String lng = edZdwd.getText().toString();
                    String lat =  edZdjd.getText().toString();
                    String description = edZdms.getText().toString();
                    String area = edZdmj.getText().toString();
                    String address = edZddz.getText().toString();
                    zoneId = Model_Regional.getRegional().getZoneId();
                    departId = Model_Regional.getRegional().getDepartId();
                    if (name.equals("") || code.equals("") || lng.equals("") || lat.equals("") || description.equals("") || area.equals("") ){
                        Toast.makeText(this, "除描述外其他为必填项", Toast.LENGTH_SHORT).show();
                    }else {
                        Addedit(name,code,lng,lat,description,area,address,zoneId,departId);
                    }
                break;
        }
    }
    public void Addedit(String name,String code,String lng,String lat,String description,String area,String address,String zoneId,String departId){
        String url = HJJF_Url.getHJJF_Url().login("api/data/site_add.do");
        Map<String,String> map = new HashMap<>();
        if (type.equals("edit")){
            url = HJJF_Url.getHJJF_Url().login("api/data/site_edit.do");
            map.put("id",detailsBean.getId());
        }
        map.put("name", name);
        map.put("zoneId", zoneId);//区域id
        map.put("code", code);
        map.put("lng", lng);
        map.put("lat", lat);
        map.put("address", address);
        map.put("area", area);
        map.put("distance", "0");
        map.put("departId", departId);//县id
        map.put("description", description);
        OkGo.<String>post(url)
                .tag(this)
                .params(map)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                            setResult(1);
                        finish();
                    }
                });
    }
}
